import axios from "axios";
import qs from "qs";
import $ from "jquery";
import { apiUrl, hncWebUrl, masterUrl, assemblyUrl,hncWebAdminUrl, ncCheckUrl, ncCheckUrlShipment } from '@/api/URLConfig';

// axios.defaults.withCredentials = true;
/* axios.defaults.withCredentials=false; */

axios.defaults.timeout = 100000;

/* 把拦截的部分先注销掉，最后做拦截 */
/* axios.interceptors.request.use(
  (config) => {
    /!*不需要获取token*!/
    let pathUrl = [
      '/index/login',
    ];
    const token = JSON.parse(window.localStorage.getItem('token')) || '';
    if (!pathUrl.some((it) => config.url.endsWith(it))) {
      if (token) {
        config.headers['Authorization'] = `${token}`;
      }
    }
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

axios.interceptors.response.use(
  function(response) {
    console.log(response.config.url)
    if (!response.config.url.endsWith('/user/get_user_by_token')) {
      // Do something with response data
      console.log(response.data)
      if (response.data.code == 9001) {
        router.push({ path: '/login' });
      }
    }
    return response;
  },
); */

// 添加请求拦截器: 让post请求的请求体格式为urlencoded格式 a=1&b=2
// 在真正发请求前执行
axios.interceptors.request.use(function(config) {
  // 得到请求方式和请求体数据
  const { method, data, headers } = config;
  let hasContentType = headers["Content-Type"];
  let isHeadersOfJson =
    headers["Content-Type"] && headers["Content-Type"] == "application/json;charset=UTF-8";
  let isHeadersOfFormData =
    headers["Content-Type"] && headers["Content-Type"] == "multipart/form-data";
  let isHeadersOfUrlencoded =
    headers["Content-Type"] &&
    headers["Content-Type"] == "application/x-www-form-urlencoded";
  let isObject = typeof data === "object";
  let isFormData = data instanceof FormData;
  if (method.toLowerCase() === "post") {
    if (isHeadersOfJson) {
    } else if (isHeadersOfFormData) {
    } else if (isHeadersOfUrlencoded) {
      config.data = qs.stringify(data);
    } else {
    }
  }
  return config;
});

// 针对云掌柜后面的页面；
const axiosPost = (url, params = {}, config) => {
  return new Promise((resolve, reject) => {
    axios
      .post(apiUrl + url, params, config)
      .then(
        response => {
          resolve(response.data);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
};

// 针对云掌柜后面的hncWeb页面；
const axiosHncPost = (url, params = {}, config) => {
  return new Promise((resolve, reject) => {
    axios
      .post(hncWebUrl + url, params, config)
      .then(
        response => {
          resolve(response.data);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
};

const axiosGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(apiUrl + url, {
        params: params
      })
      .then(
        response => {
          resolve(response.data);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
};
const axiosDelete = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .delete(hncWebUrl + url, {
        params: params
      })
      .then(
        response => {
          resolve(response.data);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
};

const cloudManagerDelete = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .delete(apiUrl + url, {
        params: params
      })
      .then(
        response => {
          resolve(response.data);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
};

const hncWebGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(hncWebUrl + url, {
        params: params
      })
      .then(
        response => {
          resolve(response.data);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
};
// 针对老版本的登录页面;
const masterGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(masterUrl + url, {
        params: params
      })
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
};

// 针对云掌柜登录页面；
// const hncWebPost = (jsonData, url) => {
//   return new Promise((resolve, reject) => {
//     $.ajax({
//       type: "post",
//       url: hncWebUrl + url,
//       contentType: "application/json",
//       data: jsonData,
//       dataType: "json",
//       cache: true,
//       async: false,
//       timeout: 60000
//     })
//       .then(
//         response => {
//           resolve(response);
//         },
//         err => {
//           reject(err);
//         }
//       )
//       .catch(error => {
//         reject(error);
//       });
//   });
// };

// 针对云掌柜登录页面；
const hncWebPost = async (url, params, config) => {
  return new Promise((resolve, reject) => {
    axios
      .post(hncWebUrl + url, params, config)
      .then(
        response => {
          resolve(response.data);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
};

// 针对云掌柜故障报修页面；
const cloudManagerPost = (jsonData, url) => {
  return new Promise((resolve, reject) => {
    $.ajax({
      type: "post",
      url: apiUrl + url,
      contentType: "application/json",
      data: jsonData,
      dataType: "json",
      cache: true,
      async: false,
      timeout: 60000
    })
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
        }
      )
      .catch(error => {
        reject(error);
      });
  });
};

// 机床装配
const assemblyGet = (url, config) => {
  return new Promise((resolve, reject) => {
    axios
      .get(assemblyUrl + url, config)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

// 机床装配
const assemblyPost = (url, params, config) => {
  return new Promise((resolve, reject) => {
    axios
      .post(assemblyUrl + url, params, config)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const hncWebAdminGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(hncWebAdminUrl + url, {
        params: params,
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const hncWebAdminPost = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .post(hncWebAdminUrl + url, params)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const ncCheckGet = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(ncCheckUrl + url, {
        params: params,
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const ncCheckPost = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .post(ncCheckUrl + url, params)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const ncCheckGetShipment = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(ncCheckUrlShipment + url, {
        params: params,
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

const ncCheckPostShipment = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .post(ncCheckUrlShipment + url, params)
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      )
      .catch((error) => {
        reject(error);
      });
  });
};

export default {
  get: axiosGet,
  post: axiosPost,
  hncPost: axiosHncPost,
  axiosDelete: axiosDelete,
  cloudManagerDelete: cloudManagerDelete,
  hncWebGet: hncWebGet,
  hncWebPost: hncWebPost,
  masterGet: masterGet,
  cloudManagerPost: cloudManagerPost,
  assemblyGet,
  assemblyPost,
  hncWebAdminGet,
  hncWebAdminPost,
  ncCheckGet,
  ncCheckPost,
  ncCheckGetShipment,
  ncCheckPostShipment
};
